﻿using System;
using System.IO;
using System.Web;
using System.Collections.Specialized;
using iSun.Entity;

namespace iSun.Web.UI.Controls
{
    public class BaseMasterPage : System.Web.UI.MasterPage 
    {

        /// <summary>
        /// 实体类名[对应表名]
        /// </summary>
        public virtual string EntityName
        {
            get
            {
                return GetStringFromQueryString("entity", "");
            }
        }


        /// <summary>
        /// 当前页面
        /// </summary>
        public virtual BasePage CureentPage
        {
            get
            {
                return this.Page as BasePage;
            }
        }

        /// <summary>
        /// 对应的编辑实体类
        /// </summary>
        public virtual string EditEntity
        {
            get
            {
                return CureentPage.EditEntity;
            }
        }




        public virtual string EntityId
        {
            get
            {
                return CureentPage.EntityId;
                //return GetStringFromQueryString("entityid", "");
            }
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            _queryString = GetQueryString();
        }


        public HttpContext ZXContext
        {
            get
            {
                return System.Web.HttpContext.Current;
            }

        }

        /// <summary>
        /// 用于加载JS脚本使用
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string LoadPageString(string filePath)
        {
            return CureentPage.LoadPageString(filePath);
            //System.IO.StringWriter sw = new StringWriter();
            //ZXContext.Server.Execute(filePath, sw);
            //return sw.GetStringBuilder().ToString();
        }

        #region Account信息

        public bool IsAuthenticated
        {
            get { return HttpContext.Current.User.Identity.IsAuthenticated; }
        }

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return HttpContext.Current.User.Identity.Name; }
        }

        /// <summary>
        /// 用户ID[]
        /// </summary>
        public Guid? UserId
        {
            get
            {
               return CureentPage.UserId;
            }
        }

        public Guid? FunctionID
        {
            get
            {
                return GetGuidFromQueryString("functionid");
            }
        }

        public DatUser User
        {
            get
            {
               return  CureentPage.User;
            }
        }

        /// <summary>
        /// 断定当前的功能权限
        /// </summary>
        /// <returns></returns>
        public bool IsExistRight()
        {
            return false;
        }

        /// <summary>
        /// 创建ToolBarItems...
        /// </summary>
        public void CreateToolBar()
        {
        }
        //在线试读功能 
        public string GetDefCondition()
        {
            return "1=1";
        }
        #endregion

        #region URL Querystring 参数
        /// <summary>
        /// 获取参数集合
        /// </summary>
        /// <returns></returns>
        public NameValueCollection GetQueryString()
        {
            return CureentPage.GetQueryString();
            //return new NameValueCollection(HttpContext.Current.Request.QueryString);
        }

        /// <summary>
        /// Allows access to QueryString values
        /// </summary>
        private NameValueCollection _queryString;
        public NameValueCollection QueryString
        {
            get
            {
                return GetQueryString();
            }
        }


        // *********************************************************************
        //  获取Guid参数
        /// <summary>
        /// Retrieves a value from the query string and returns it as an int.
        /// </summary>
        // ***********************************************************************/
        public Guid GetGuidFromQueryString(string key)
        {
            return CureentPage.GetGuidFromQueryString(key);

        }

        // *********************************************************************
        //  获取整型参数
        //
        /// <summary>
        /// Retrieves a value from the query string and returns it as an int.
        /// </summary>
        // ***********************************************************************/
        public int GetIntFromQueryString(string key, int defaultValue)
        {
            return CureentPage.GetIntFromQueryString(key, defaultValue);
         }

        /// <summary>
        /// 获取字符参数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public string GetStringFromQueryString(string key, string defaultValue)
        {
            return CureentPage.GetStringFromQueryString(key, defaultValue); 
        }
        #endregion
    }
}
